package global.com.loansapplication.ui.fragment.presenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import global.com.loansapplication.base.BaseBean;
import global.com.loansapplication.base.BaseBiz;
import global.com.loansapplication.bean.ClueInfo;
import global.com.loansapplication.bean.SettingInfo;
import global.com.loansapplication.comm.Constants;
import global.com.loansapplication.comm.MyApplication;
import global.com.loansapplication.ui.actviity.login.SelcetInfoAct;
import global.com.loansapplication.ui.actviity.user.OwnerAct;
import global.com.loansapplication.ui.fragment.biz.GrabHallBiz;
import global.com.loansapplication.ui.fragment.contract.GrabHallContract;

public class GrabHallPresent implements GrabHallContract.Presenter {

    private GrabHallContract.View mView;
    private GrabHallBiz mBiz;

    private int index = 1;
    private List<ClueInfo> clueInfoList = new ArrayList<>();

    @Inject
    public GrabHallPresent(GrabHallContract.View view) {
        this.mView = view;
    }

    @Override
    public void setBiz(BaseBiz biz) {
        this.mBiz = (GrabHallBiz) biz;
    }

    @Override
    public void toOwner() {
        mView.startAct(OwnerAct.class, null);
    }

    @Override
    public void toSelect() {
        mView.startAct(SelcetInfoAct.class, null);
    }

    @Override
    public void getList(SettingInfo settingInfo) {
        mView.loading(true);
        mBiz.getClueList(MyApplication.userInfo.getUserCode(), index, Constants.PAGESIZE, settingInfo.getCityIds(), settingInfo.getCustomerType(), settingInfo.getMonth(), settingInfo.gethavecar(),
                settingInfo.gethavehouse(), settingInfo.gethaveCreditCard(), settingInfo.gethaveShebao(), settingInfo.gethaveGongjijin(), settingInfo.gethaveBaodan(), settingInfo.gethaveYibao(), new BaseBiz.Callback<BaseBean<List<ClueInfo>>>() {
                    @Override
                    public void onSuccess(BaseBean<List<ClueInfo>> o) {
                        clueInfoList.addAll(o.getList());
                        mView.updateList(clueInfoList);
                        if (index > 1) {
                            mView.loadMoreComplete();
                        } else {
                            mView.refreshComplete();
                        }
                        mView.loading(false);
                        mView.hasLoadMore(index * Constants.PAGESIZE >= o.getTotal());
                        index++;
                    }

                    @Override
                    public void onFailure(BaseBean<List<ClueInfo>> o) {
                        if (index > 1) {
                            mView.loadMoreComplete();
                        } else {
                            mView.refreshComplete();
                        }
                        mView.loading(false);
                    }
                });
    }

    @Override
    public void refresh() {
        index = 1;
        clueInfoList.clear();
        mView.updateList(clueInfoList);
    }
}
